package com.yisen.dasan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yisen.dasan.dao.AnalyzeDataMapper;
import com.yisen.dasan.service.AnalyzeDataService;
import com.yisen.dasan.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class AnalyzeDataServiceImpl implements AnalyzeDataService {
    @Autowired
    AnalyzeDataMapper analyzeDataMapper;
    @Override
    public Object redioAnalyze(int itemId) {
        return analyzeDataMapper.redioAnalyze(itemId);
    }
    @Override
    public Object genderAnalyze(int itemId) {
        return analyzeDataMapper.genderAnalyze(itemId);
    }
    @Override
    public List<Map> multipleAnalyze(int itemId){
        return analyzeDataMapper.multipleAnalyze(itemId);
    }
    @Override
    public int[] totalAnalyze(int formId){
        List<Map> list = analyzeDataMapper.totalAnalyze(formId);
        int Mon=0,Tue=0,Wed=0,Thu=0,Fri=0,Sat=0,Sun=0;
        for(Map map : list){
            long time= Long.parseLong(map.get("date").toString());
            int week=TimeUtils.getWeek(time);
            System.out.println(week);
            if(week==1){ Mon++; }
            else if(week==2){ Tue++; }
            else if(week==3){ Wed++; }
            else if(week==4){ Thu++; }
            else if(week==5){ Fri++; }
            else if(week==6){ Sat++; }
            else if(week==7){ Sun++; }
        }
        int[] count={Mon,Tue,Wed,Thu,Fri,Sat,Sun};
        return count;
    }
    @Override
    public List<Map> mapAnalyze(int formId){
        List<Map> list = analyzeDataMapper.mapAnalyze(formId);
        return list;
    }

    @Override
    public JSONObject indexAnalyze(int formId) {
        JSONObject result = new JSONObject();// 声明要返回的对象
        // 获取时间戳
        LocalDate localDate = LocalDate.now();// 用来获取今天的日期
        LocalDateTime time = LocalDateTime.of(
                localDate.getYear(),
                localDate.getMonth(),
                localDate.getDayOfMonth(),
                0, 0, 0);// 拿到一个今天 0点0分0秒的对象
        long l = time.toEpochSecond(ZoneOffset.ofHours(8))*1000;// 拿到时间戳
        List<Map<String, Object>> list = analyzeDataMapper.indexAnalyze(formId);
        Stream<Map<String, Object>> stream = list.stream();// 转为流处理
        int n = stream.mapToInt(obj -> {
            System.out.println(obj.get("date"));
            long i = (long) obj.get("date");// 提交时间
            if(i>l){
                return 1;
            }
            return 0;
        }).sum();// 取新增数量
        stream = list.stream();// 转为流处理
        int averageTime = (int) stream.collect(Collectors.summarizingInt(obj -> {
            int time1 = (Integer) obj.get("time");
            return time1;
        })).getAverage();// 取平均时间
        result.put("total", list.size());
        result.put("count", list.get(0).get("count"));
        result.put("new", n);
        result.put("time", averageTime);
        return result;
    }
}
